import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatefulWidget {
  @override
  _HYHomePageState createState() => _HYHomePageState();
}

class _HYHomePageState extends State<HYHomePage>
    with SingleTickerProviderStateMixin {
  // 创建AnimationController
  AnimationController _controller;
  Animation _animation;
  Animation _sizeAnimation;

  @override
  void initState() {
    super.initState();

    // 1.创建AnimationController
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
      // lowerBound: 50,
      // upperBound: 150,
    );

    // 2.设置Curve的值
    _animation = CurvedAnimation(parent: _controller, curve: Curves.linear);

    //3. Tween
    _sizeAnimation = Tween(begin: 50.0, end: 150.0).animate(_animation);

    // 监听动画值的改变
    _controller.addListener(() {
      setState(() {});
    });

    // 监听动画的状态改变
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: Icon(Icons.favorite,
              color: Colors.red, size: _sizeAnimation.value),
        ),
        backgroundColor: Colors.white,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.play_arrow),
            backgroundColor: Colors.teal,
            onPressed: () {
              // _controller.forward();

              if (_controller.isAnimating) {
                _controller.stop();
                print(_controller.status);
              } else if (_controller.status == AnimationStatus.forward) {
                print(_controller.status);
                _controller.forward();
              } else if (_controller.status == AnimationStatus.reverse) {
                _controller.reverse();
              } else {
                _controller.forward();
              }
            }),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

class HYHomePageDemo extends StatefulWidget {
// class HYHomePageDemo extends StatelessWidget {
  _HYHomePageDemoState createState() => _HYHomePageDemoState();
}

class _HYHomePageDemoState extends State<HYHomePageDemo>
    with SingleTickerProviderStateMixin {
  ///  1.Animation：抽象类
  ///    * 监听动画值的改变
  ///    * 监听动画状态的改变
  ///    * value
  ///    * status
  ///  2.AnimationController继承自Animation
  ///    * vsync: 同步信号（this->with SingleTickerProviderStateMixin）
  ///    * forward():向前执行动画
  ///    * reverse(): 反转执行动画
  ///  3.CurvedAnimation:
  ///    * 作用: 设置动画执行的速率(速度曲线)
  ///    *
  ///  4.Tween: 设置动画执行的value范围
  ///

  @override
  Widget build(BuildContext context) {
    final controller = AnimationController(vsync: this);
    final animation =
    CurvedAnimation(parent: controller, curve: Curves.elasticInOut);
    // Tween(begin: 100, end: 200).animate(controller);
    final valueAnimation = Tween(begin: 100, end: 200).animate(animation);

    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Text('Hello Flutter'),
        backgroundColor: Colors.white,
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            backgroundColor: Colors.teal,
            onPressed: () => print('FloatingActionButton click')),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }
}
